package com.saifan.wyy_ov.ui.communitycenter;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.FleaMarketInfoBean;
import com.saifan.wyy_ov.data.bean.RequestBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.LabelView;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.DensityUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-6-5 17:11:08
 * @descrption 我的
 */

public class MyFleaMarketRecordActivity extends BaseActivity {
    private Toolbar toolbar;
    private ListView listView;

    List<FleaMarketInfoBean> rentalBeans = new ArrayList<>();
    private CommonAdapter<FleaMarketInfoBean> adapter;
    private RequestBean requestBean;
    private GetData getData;
    private View loadlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        requestBean = new RequestBean();
        getData = new GetData();
        initAdapter();

    }

    @Override
    protected void onStart() {
        super.onStart();
        query();
    }

    public void setContentView() {
        setContentView(R.layout.activity_my_rental_record);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        loadlay = findViewById(R.id.loading_lay);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    public void query() {
        loadlay.setVisibility(View.VISIBLE);
        requestBean.setTZSC_KHWJ(getRoomInfoBean().getWYKHDA_ZJ());
        getData.getDataJson(this, Api.FleaMarket.PERSONAL_FLEA_MARKET_INFO, requestBean, getString(R.string.wait), new ResponseData() {
            @Override
            public void onSuccess(String json) {
                List<FleaMarketInfoBean> list = new Gson().fromJson(json, new TypeToken<List<FleaMarketInfoBean>>() {
                }.getType());
                if (list != null && !list.isEmpty()) {
                    adapter.mDatas = list;
                    adapter.notifyDataSetChanged();


                }else {
                    ToastUtil.showToast(MyFleaMarketRecordActivity.this
                            ,"暂无相关信息");
                }
                loadlay.setVisibility(View.GONE);
            }

            @Override
            public void onFailed(String str) {
                loadlay.setVisibility(View.GONE);
            }
        });
    }

    public void initAdapter() {
        adapter = new CommonAdapter<FleaMarketInfoBean>(this, rentalBeans, R.layout.my_fleamarket_item) {
            @Override
            public void convert(ViewHolderHelper helper, FleaMarketInfoBean item) {


                helper.setText(R.id.LXR, StringUtil.returnShow(item.getTZSC_LXR()));
                if (item.getTZSC_LX() == 1) {
                    helper.setText(R.id.LX, "转让");
                } else if (item.getTZSC_LX() == 2) {
                    helper.setText(R.id.LX, "置换");
                } else if (item.getTZSC_LX() == 3) {
                    helper.setText(R.id.LX, "赠送");
                }
                helper.setText(R.id.BT, StringUtil.returnShow(item.getTZSC_BT()));
                helper.setText(R.id.SJ, StringUtil.returnShow(item.getTZSC_FBSJ()));


                LinearLayout targetView = helper.getView(R.id.layout);

                int window_width_dp = getWindowManager().getDefaultDisplay().getWidth();
                int window_width_px = DensityUtil.px2dip(MyFleaMarketRecordActivity.this, window_width_dp) - 20;
                LabelView label = helper.getView(12345);
                if (label == null) {
                    label = new LabelView(MyFleaMarketRecordActivity.this);
                    label.setId(12345);
                    label.setHeight(DensityUtil.dip2px(MyFleaMarketRecordActivity.this, 40));
                    label.setWidth(DensityUtil.dip2px(MyFleaMarketRecordActivity.this, 120));
                    label.setGravity(Gravity.BOTTOM | Gravity.CENTER);

                }
                if (!item.isTZSC_SH() && !StringUtil.isNullOrEmpty(item.getTZSC_WTGYY())) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable1));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("未通过");
                    helper.getView(R.id.BSLYY).setVisibility(View.VISIBLE);
                    helper.setText(R.id.BSLYY, "未通过原因：" + StringUtil.returnShow(item.getTZSC_WTGYY()));
                } else if (!item.isTZSC_SH() && StringUtil.isNullOrEmpty(item.getTZSC_WTGYY())) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable2));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("审核中");
                    helper.getView(R.id.BSLYY).setVisibility(View.GONE);
                } else if (item.isTZSC_SH() && StringUtil.isNullOrEmpty(item.getTZSC_WTGYY())) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable3));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("已通过");
                    helper.getView(R.id.BSLYY).setVisibility(View.GONE);
                }


            }
        };

        listView.setAdapter(adapter);

    }


}
